package util;


import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.zin.viewpagerfragment.R;


import org.apache.http.util.EncodingUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Date;
import java.text.SimpleDateFormat;


public class Utils {

    public static Context context;

    public static void init(Context cxt){
        context = cxt;
    }
    /**
     * 短时间显示Toast
     */
    public static void show(String msg){
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    private static BitmapUtils bitmapUtils = null;

    public static BitmapUtils getInstance(){
        if (bitmapUtils == null) {
            bitmapUtils = new BitmapUtils(context);
            bitmapUtils.configDefaultLoadingImage(R.drawable.news_pic_default);
            bitmapUtils.configDefaultLoadFailedImage(R.drawable.news_pic_default);
            bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);

            bitmapUtils.configMemoryCacheEnabled(true);
            bitmapUtils.configDiskCacheEnabled(true);
        }

        return bitmapUtils;
    }


    // 读SD中的文件
    /**
     * 读SD中的文件
     * @param fileName
     * @return
     */
    public static String readFileSdcardFile(String fileName) {
        String res = "";
        try {
            FileInputStream fin = new FileInputStream(fileName);

            int length = fin.available();

            byte[] buffer = new byte[length];
            fin.read(buffer);

            res = EncodingUtils.getString(buffer, "UTF-8");

            fin.close();
        }

        catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    // 写数据到SD中的文件
    /**
     * 写数据到SD中的文件
     * @param fileName
     * @param write_str
     */
    public static void writeFileSdcardFile(String fileName, String write_str) {
        try {

            FileOutputStream fout = new FileOutputStream(fileName);
            byte[] bytes = write_str.getBytes();

            fout.write(bytes);
            fout.close();
        }

        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 日期格式转换
     * @param millis
     * @return
     */
    public static String getTime(long millis){
        Date date = new Date(millis);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 判断当前网络(有/无)
     * @return
     */
    public static boolean hasConnected(){
        boolean res = false;
        ConnectivityManager connectivityManager
                =(ConnectivityManager)context.getSystemService(
                context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if(networkInfo == null || !networkInfo.isAvailable())
        {
            //当前无可用网络
            res = false;
        }
        else
        {
            //当前有可用网络
            res = true;
        }

        return res;

    }




}
